
use "Temp/Master_5yr.dta" , clear
xtset iso_n demidecade

 
 gen high_democracy=1 if  libdem_extra_vdem>=012.98 
 
 replace high_democracy=0 if high_democracy==.
 
 
  * set graph off to surpress interflex plot (just used for calculated Hainmueller et al's p
 set graphics off
 
 
  
 
 
******* (1) Decolonialisation 

 	eststo m1: reghdfe dtax_non_trade_real   l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged   P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv libdem_extra_vdem	X_* 	  c.l1_gov_change decol   , absorb(year iso_n) cluster(     iso_n)
	
	estadd scalar N_true = e(N) + e(num_singletons)
	
	
	
	
	// (2) Access to ODA
	
	
					* calculate marginal effects and save them and joint pvalues into scalars
					eststo m5: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged    P_ind_total_f_realshare      cr_market_accessXBOEinv X_*  decol libdem_extra_vdem c.l1_gov_change##i.S_g5_unw_alliance_abs_binned     , absorb(year iso_n) cluster(   iso_n)
						tempfile holding
						margins S_g5_unw_alliance_abs_binned  , dydx(l1_gov_change)   post saving(`holding')
						clonevar _m1 = S_g5_unw_alliance_abs_binned
						merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
						drop _m1 _merge
						
						 
						
						
						egen temp_marg_low=mean(_margin) if S_g5_unw_alliance_abs_binned==0
						egen temp_marg_medium=mean(_margin) if S_g5_unw_alliance_abs_binned==1						
						egen temp_marg_high=mean(_margin) if S_g5_unw_alliance_abs_binned==2
						
						egen temp_p_low=mean(_pvalue) if S_g5_unw_alliance_abs_binned==0
						egen temp_p_medium=mean(_pvalue) if S_g5_unw_alliance_abs_binned==1
						egen temp_p_high=mean(_pvalue) if S_g5_unw_alliance_abs_binned==2					
						
						egen margin_low=mean(temp_marg_low)
						egen margin_medium=mean(temp_marg_medium)
						egen margin_high=mean(temp_marg_high)

						egen p_low=mean(temp_p_low)
						egen p_medium=mean(temp_p_medium)
						egen p_high=mean(temp_p_high)
											
						scalar p_low_s=p_low
						scalar p_medium_s=p_medium
						scalar p_high_s=p_high
	
					* Wald statistic for linear vs. binned model
					interflex dtax_non_trade_real l1_gov_change    S_g5_unw_alliance_abs libdem_extra_vdem l1_int_war_all_PRIO l1_civ_war_all_PRIO   P_ind_total_f_realshare   S_g5_unw_alliance_abs   cr_market_accessXBOEinv l1_drought_affected_merged  X_* decol , fe(year iso_n) cl(iso_n) 
					scalar   l_P_wald =  r(pwald)

					* actual model 	
					eststo m5: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged    P_ind_total_f_realshare      cr_market_accessXBOEinv X_* decol  libdem_extra_vdem c.l1_gov_change##i.S_g5_unw_alliance_abs_binned     , absorb(year iso_n) cluster(   iso_n)
						
							* add statistics to table
							scalar pval_moderator_medium = (2 * ttail(e(df_r), abs(_b[1.S_g5_unw_alliance_abs_binned] / _se[1.S_g5_unw_alliance_abs_binned]) ) )
							scalar pval_moderator_high = (2 * ttail(e(df_r), abs(_b[2.S_g5_unw_alliance_abs_binned] / _se[2.S_g5_unw_alliance_abs_binned]) ) )
							estadd scalar N_true = e(N) + e(num_singletons)
							estadd scalar P_wald = l_P_wald
							estadd local	moderator_medium  	"`= cond(pval_moderator_medium<0.01,"`:di %5.3f `=_b[1.S_g5_unw_alliance_abs_binned]''***", 	cond(pval_moderator_medium<0.05,"`:di %5.2f `=_b[1.S_g5_unw_alliance_abs_binned]''**", 	cond(pval_moderator_medium<0.1,"`:di %5.2f `=_b[1.S_g5_unw_alliance_abs_binned]''*",  "`:di %5.2f `=_b[1.S_g5_unw_alliance_abs_binned]''")))'"					
							estadd local	moderator_high  	"`= cond(pval_moderator_high<0.01,"`:di %5.3f `=_b[2.S_g5_unw_alliance_abs_binned]''***", 		cond(pval_moderator_high<0.05,"`:di %5.2f `=_b[2.S_g5_unw_alliance_abs_binned]''**", 	cond(pval_moderator_high<0.1,"`:di %5.2f `=_b[2.S_g5_unw_alliance_abs_binned]''*",  "`:di %5.2f `=_b[2.S_g5_unw_alliance_abs_binned]''")))'"
							estadd scalar moderator_medium_se = _se[1.S_g5_unw_alliance_abs_binned] 
							estadd scalar moderator_high_se =  _se[2.S_g5_unw_alliance_abs_binned] 

 
  							* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.3f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_medium		=  	 "`= cond(p_medium_s<0.01,"`:di %5.3f `=margin_medium''***", 	cond(p_medium_s<0.05,"`:di %5.2f `=margin_medium''**", 	cond(p_medium_s<0.1,"`:di %5.2f `=margin_medium''*",  "`:di %5.2f `=margin_medium''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.3f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
					
					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high temp_marg_medium temp_p_medium margin_medium p_medium
					
		
 
 

	
	
	 	// (3) Access to credit

					* calculate marginal effects and save them and joint pvalues into scalars
						eststo m6: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged       S_g5_unw_alliance_abs     X_*  decol libdem_extra_vdem c.l1_gov_change##i.cr_market_accessXBOEinv_binned     , absorb(year iso_n) cluster(   iso_n)
						tempfile holding
						margins cr_market_accessXBOEinv_binned  , dydx(l1_gov_change)   post saving(`holding')
						clonevar _m1 = cr_market_accessXBOEinv_binned
						merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
						drop _m1 _merge
						
						 
						
						
						egen temp_marg_low=mean(_margin) if cr_market_accessXBOEinv_binned==0
						egen temp_marg_medium=mean(_margin) if cr_market_accessXBOEinv_binned==1						
						egen temp_marg_high=mean(_margin) if cr_market_accessXBOEinv_binned==2
						
						egen temp_p_low=mean(_pvalue) if cr_market_accessXBOEinv_binned==0
						egen temp_p_medium=mean(_pvalue) if cr_market_accessXBOEinv_binned==1
						egen temp_p_high=mean(_pvalue) if cr_market_accessXBOEinv_binned==2					
						
						egen margin_low=mean(temp_marg_low)
						egen margin_medium=mean(temp_marg_medium)
						egen margin_high=mean(temp_marg_high)

						egen p_low=mean(temp_p_low)
						egen p_medium=mean(temp_p_medium)
						egen p_high=mean(temp_p_high)
											
						scalar p_low_s=p_low
						scalar p_medium_s=p_medium
						scalar p_high_s=p_high
						
						
						* Wald statistic for linear vs. binned model
						interflex dtax_non_trade_real l1_gov_change    cr_market_accessXBOEinv l1_int_war_all_PRIO l1_civ_war_all_PRIO  libdem_extra_vdem    S_g5_unw_alliance_abs P_ind_total_f_realshare    l1_drought_affected_merged  X_* decol, fe(year iso_n) cl(iso_n) 
						mat list r(estBin)
						scalar   l_P_wald =  r(pwald)

						* actual model 	
						eststo m6: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged       S_g5_unw_alliance_abs     X_* decol libdem_extra_vdem c.l1_gov_change##i.cr_market_accessXBOEinv_binned     , absorb(year iso_n) cluster(   iso_n)
						
						
							* add statistics to table
							scalar pval_moderator_medium = (2 * ttail(e(df_r), abs(_b[1.cr_market_accessXBOEinv_binned] / _se[1.cr_market_accessXBOEinv_binned]) ) )
							scalar pval_moderator_high = (2 * ttail(e(df_r), abs(_b[2.cr_market_accessXBOEinv_binned] / _se[2.cr_market_accessXBOEinv_binned]) ) )
							estadd scalar N_true = e(N) + e(num_singletons)
							estadd scalar P_wald = l_P_wald
							estadd local	moderator_medium  "`= cond(pval_moderator_medium<0.01,"`:di %5.3f `=_b[1.cr_market_accessXBOEinv_binned]''***", 	cond(pval_moderator_medium<0.05,"`:di %5.2f `=_b[1.cr_market_accessXBOEinv_binned]''**", 	cond(pval_moderator_medium<0.1,"`:di %5.2f `=_b[1.cr_market_accessXBOEinv_binned]''*",  "`:di %5.2f `=_b[1.cr_market_accessXBOEinv_binned]''")))'"					
							estadd local	moderator_high  "`= cond(pval_moderator_high<0.01,"`:di %5.3f `=_b[2.cr_market_accessXBOEinv_binned]''***", 		cond(pval_moderator_high<0.05,"`:di %5.2f `=_b[2.cr_market_accessXBOEinv_binned]''**", 	cond(pval_moderator_high<0.1,"`:di %5.2f `=_b[2.cr_market_accessXBOEinv_binned]''*",  "`:di %5.2f `=_b[2.cr_market_accessXBOEinv_binned]''")))'"
							estadd scalar moderator_medium_se = _se[1.cr_market_accessXBOEinv_binned] 
							estadd scalar moderator_high_se =  _se[2.cr_market_accessXBOEinv_binned] 
 
  							* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.3f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_medium		=  	 "`= cond(p_medium_s<0.01,"`:di %5.3f `=margin_medium''***", 	cond(p_medium_s<0.05,"`:di %5.2f `=margin_medium''**", 	cond(p_medium_s<0.1,"`:di %5.2f `=margin_medium''*",  "`:di %5.2f `=margin_medium''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.3f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
					
					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high temp_marg_medium temp_p_medium margin_medium p_medium
					
		
		 					
												
	 

 	// (4) Interaction with resource prices  
	
	
					* calculate marginal effects and save them and joint pvalues into scalars
					eststo m4: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged      S_g5_unw_alliance_abs   cr_market_accessXBOEinv X_* decol libdem_extra_vdem   c.l1_gov_change##i.P_ind_total_f_realshare_binned    , absorb(year iso_n) cluster(   iso_n)
						tempfile holding
						margins P_ind_total_f_realshare_binned  , dydx(l1_gov_change)   post saving(`holding')
						clonevar _m1 = P_ind_total_f_realshare_binned
						merge m:1 _m1 using `holding',  keepusing(_margin _pvalue)
						drop _m1 _merge
						
						 
						
						
						egen temp_marg_low=mean(_margin) if P_ind_total_f_realshare_binned==0
						egen temp_marg_medium=mean(_margin) if P_ind_total_f_realshare_binned==1						
						egen temp_marg_high=mean(_margin) if P_ind_total_f_realshare_binned==2
						
						egen temp_p_low=mean(_pvalue) if P_ind_total_f_realshare_binned==0
						egen temp_p_medium=mean(_pvalue) if P_ind_total_f_realshare_binned==1
						egen temp_p_high=mean(_pvalue) if P_ind_total_f_realshare_binned==2					
						
						egen margin_low=mean(temp_marg_low)
						egen margin_medium=mean(temp_marg_medium)
						egen margin_high=mean(temp_marg_high)

						egen p_low=mean(temp_p_low)
						egen p_medium=mean(temp_p_medium)
						egen p_high=mean(temp_p_high)
											
						scalar p_low_s=p_low
						scalar p_medium_s=p_medium
						scalar p_high_s=p_high
	
	
	
	
	
	
					* Wald statistic for linear vs. binned model
					interflex dtax_non_trade_real l1_gov_change    P_ind_total_f_realshare l1_int_war_all_PRIO l1_civ_war_all_PRIO   libdem_extra_vdem   S_g5_unw_alliance_abs   cr_market_accessXBOEinv l1_drought_affected_merged  X_* decol, fe(year iso_n) cl(iso_n) 
					scalar   l_P_wald =  r(pwald)
	
	
					* actual model 	
					eststo m4: reghdfe dtax_non_trade_real      l1_int_war_all_PRIO l1_civ_war_all_PRIO l1_drought_affected_merged      S_g5_unw_alliance_abs   cr_market_accessXBOEinv X_*  decol libdem_extra_vdem   c.l1_gov_change##i.P_ind_total_f_realshare_binned    , absorb(year iso_n) cluster(   iso_n)
					
							* add statistics to table
							scalar pval_moderator_medium = (2 * ttail(e(df_r), abs(_b[1.P_ind_total_f_realshare_binned] / _se[1.P_ind_total_f_realshare_binned]) ) )
							scalar pval_moderator_high = (2 * ttail(e(df_r), abs(_b[2.P_ind_total_f_realshare_binned] / _se[2.P_ind_total_f_realshare_binned]) ) )
							estadd scalar N_true = e(N) + e(num_singletons)
							estadd scalar P_wald = l_P_wald
							estadd local	moderator_medium  "`= cond(pval_moderator_medium<0.01,"`:di %5.3f `=_b[1.P_ind_total_f_realshare_binned]''***", 	cond(pval_moderator_medium<0.05,"`:di %5.2f `=_b[1.P_ind_total_f_realshare_binned]''**", 	cond(pval_moderator_medium<0.1,"`:di %5.2f `=_b[1.P_ind_total_f_realshare_binned]''*",  "`:di %5.2f `=_b[1.P_ind_total_f_realshare_binned]''")))'"					
							estadd local	moderator_high  "`= cond(pval_moderator_high<0.01,"`:di %5.3f `=_b[2.P_ind_total_f_realshare_binned]''***", 	cond(pval_moderator_high<0.05,"`:di %5.2f `=_b[2.P_ind_total_f_realshare_binned]''**", 	cond(pval_moderator_high<0.1,"`:di %5.2f `=_b[2.P_ind_total_f_realshare_binned]''*",  "`:di %5.2f `=_b[2.P_ind_total_f_realshare_binned]''")))'"
							estadd scalar moderator_medium_se = _se[1.P_ind_total_f_realshare_binned] 
							estadd scalar moderator_high_se =  _se[2.P_ind_total_f_realshare_binned] 
							
 							* add marginal effects with significance stars
							estadd local margin_low		=  	 "`= cond(p_low_s<0.01,"`:di %5.3f `=margin_low''***", 	cond(p_low_s<0.05,"`:di %5.2f `=margin_low''**", 	cond(p_low_s<0.1,"`:di %5.2f `=margin_low''*",  "`:di %5.2f `=margin_low''")))'"					
							estadd local margin_medium		=  	 "`= cond(p_medium_s<0.01,"`:di %5.3f `=margin_medium''***", 	cond(p_medium_s<0.05,"`:di %5.2f `=margin_medium''**", 	cond(p_medium_s<0.1,"`:di %5.2f `=margin_medium''*",  "`:di %5.2f `=margin_medium''")))'"					
							estadd local margin_high	=  	 "`= cond(p_high_s<0.01,"`:di %5.3f `=margin_high''***", 	cond(p_high_s<0.05,"`:di %5.2f `=margin_high''**", 	cond(p_high_s<0.1,"`:di %5.2f `=margin_high''*",  "`:di %5.2f `=margin_high''")))'"					
					
					
					
					
									* drop temporary variables
									drop _margin _pvalue temp_marg_low temp_marg_high temp_p_low temp_p_high margin_low margin_high p_low p_high temp_marg_medium temp_p_medium margin_medium p_medium
					
		
		 


 			
 
 
 
 esttab   m1      m5 m6  m4 using "Output/Tables/Main/Table2.rtf", replace style(tex) cells(b(star fmt(2)) se(par fmt(2)) P_wald(  fmt(2))) stats(margin_low margin_medium margin_high moderator_medium moderator_medium_se moderator_high moderator_high_se  N_true r2_a    P_wald, fmt(2 2 2 2  2  2  2  0 2 2 ) labels("Marginal effect (moderator low)"    "Marginal effect (moderator medium)" "Marginal effect (moderator high)" "Moderator - medium" " " "Moderator - high" " " "Observations" "Adjusted  R^2 " "Xu's Wald test")) legend label   starlevels(* 0.1 ** 0.05 *** 0.01) drop(_cons l1_int_war_all_PRIO libdem_extra_vdem l1_civ_war_all_PRIO l1_drought_affected_merged  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_*)
  
 esttab   m1      m5 m6  m4 using "Output/Tables/Main/Table2.tex", replace style(tex) cells(b(star fmt(2)) se(par fmt(2)) P_wald(  fmt(2))) stats(margin_low margin_medium margin_high moderator_medium   moderator_high    N_true r2_a    P_wald, fmt(2 2 2    2  2  0 2 2 ) labels("Marginal effect (moderator low)"    "Marginal effect (moderator medium)" "Marginal effect (moderator high)" "Moderator - medium"  "Moderator - high"  "Observations" "Adjusted  R^2 " "HMX Wald test")) legend label   starlevels(* 0.1 ** 0.05 *** 0.01) drop(_cons l1_int_war_all_PRIO libdem_extra_vdem l1_civ_war_all_PRIO l1_drought_affected_merged  P_ind_total_f_realshare  S_g5_unw_alliance_abs   cr_market_accessXBOEinv 	X_*)


 
 
set graphics on
